﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Salon
{
    public partial class RegisterUsers : Form
    {
        public RegisterUsers()
        {
            InitializeComponent();
            dgvUserGrid.AutoGenerateColumns = false;
        }


        private void RegisterUsers_Load(object sender, EventArgs e)
        {
            FillTable();
        }

        

        private void FillTable()
        {
            BindingSource bs = new BindingSource();
            UserService us = new UserService();
            IList<User> users = us.getUsers(Global.getFilterList(this));
            foreach (User user in users)
                bs.Add(user);

            dgvUserGrid.DataSource = bs;
        }
        

        private void dgvUserGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!Global.MainPage.checkIfFormOpen(Forms.USER_FORM))
            {
                FormUser fu = new FormUser((User)dgvUserGrid.Rows[e.RowIndex].DataBoundItem,false);
                fu.MdiParent = Global.MainPage;
                fu.Text = Forms.USER_FORM;
                fu.Show();
            }
            else
            { 
                
            }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            FillTable();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            foreach (IFilter filtr in Global.getFilterList(this))
            {
                filtr.CleanUp();
            }
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            Global.MainPage.openNewUserForm(null,false);
        }

        private void btnEditUser_Click(object sender, EventArgs e)
        {
            if (dgvUserGrid.SelectedRows.Count != 1)
            {
                MessageBox.Show("Proszę zaznaczyć dokładnie jeden wiersz do edycji.");
                return;
            }

            Global.MainPage.openNewUserForm((User)dgvUserGrid.SelectedRows[0].DataBoundItem, false);
        }

        private void btnDeleteUser_Click(object sender, EventArgs e)
        {
            if (dgvUserGrid.SelectedRows.Count < 1)
            {
                MessageBox.Show("Proszę zaznaczyć przynajmniej jeden wiersz do usunięcia.");
                return;
            }

            if (MessageBox.Show("Czy na pewno chcesz usunąć wszystkich zaznaczonych użytkowników?","", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                UserService us = new UserService();
                for (int i = 0; i < dgvUserGrid.SelectedRows.Count; i++)
                    us.deleteUser((User)dgvUserGrid.SelectedRows[i].DataBoundItem);
                FillTable();
            }
        }

        
    }
}
